package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类业务层
 */
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    /**
     * 分类分页查询
     * <p>
     * //     * @param name
     * //     * @param page
     * //     * @param pageSize
     *
     * @param categoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageList(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());

        Page<Category> categoryPage = (Page<Category>) categoryMapper.pageList(categoryPageQueryDTO.getName(),
                categoryPageQueryDTO.getType());

        return new PageResult(categoryPage.getTotal(), categoryPage.getResult());
    }

    /**
     * 根据类型查询分类
     *
     * @param type
     * @return
     */
    @Override
    public List<Category> list(Integer type) {
        List<Category> list = categoryMapper.list(type);

        return list;
    }

    /**
     * 启用、禁用分类
     *
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Long id) {


        categoryMapper.updateStatus(status, id);
    }

    /**
     * 根据id删除分类
     *
     * @param categoryDTO
     */
    @Override
    public void delete(CategoryDTO categoryDTO) {
        Category category = categoryMapper.findByCondition(categoryDTO);
        if (category.getStatus() == 1) {
            throw new BaseException("启用状态不能删除");
        }

        DishPageQueryDTO dto = new DishPageQueryDTO();
        dto.setCategoryId(categoryDTO.getId());
        List<DishVO> dishVOS = dishMapper.pageList(dto);
        if (dishVOS.size() != 0) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH);
        }

        SetmealPageQueryDTO setmealPageQueryDTO = new SetmealPageQueryDTO();
        setmealPageQueryDTO.setCategoryId(categoryDTO.getId());
        List<SetmealVO> setmealVOS = setmealMapper.pageList(setmealPageQueryDTO);
        if (setmealVOS.size() != 0) {
            throw new BaseException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL);
        }

        categoryMapper.delete(category.getId());
    }

    /**
     * 新增分类
     *
     * @param categoryDTO
     */
    @Override
    public void add(CategoryDTO categoryDTO) {
        Category category = Category.builder()
                .name(categoryDTO.getName())
                .sort(categoryDTO.getSort())
                .type(categoryDTO.getType())
                .status(StatusConstant.ENABLE)
                .build();

        categoryMapper.add(category);
    }

    /**
     * 修改分类信息
     *
     * @param categoryDTO
     */
    @Override
    public void updateInfo(CategoryDTO categoryDTO) {
        CategoryDTO dto = new CategoryDTO();
        dto.setName(categoryDTO.getName());

        Category category = categoryMapper.findByCondition(dto);
        if (category != null && !categoryDTO.getId().equals(category.getId())) {
            throw new BaseException("分类名称不能重复");
        }

        categoryMapper.updateInfo(categoryDTO);
    }
}
